﻿using System;

namespace School.Model
{
    using EDP.Common.Model;
    using EDP.Common.Help;

    using EDP.Database;
    using EDP.Framework;

    using EDP.Authentication;    

    /// <summary>
    /// 学生业务对象
    /// </summary>
    [ModelEnitity(TableName = "t_school_student", KeyName = "ID")]
    public class StudentModel: ModelPermissionBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public StudentModel() {
            this.SetDefaultQuerySQL(@"select * 
	from (select a.ID
			,a.Name
			,a.Number
			,a.GenderId
			,a.Age
			,a.Contact
			,a.ContactMobile
			,a.Hobby
			,a.CampusId
			,a.ClassId
			,a.CreateUserId
			,a.CreateUserName
			,a.CreateDepartmentId
			,a.CreateOrganizationId
			,a.CreateTime
			,a.ModifyUserId
			,a.ModifyUserName
			,a.ModifyTime
			,a.Remarks
			,b.Name CampusName
			,c.Name ClassName
			,d.Name SchoolName
			,c.FullCode ClassFullCode
		from t_school_student a
		left join t_sys_organization_unit b on a.CampusId = b.ID
		left join t_sys_organization_unit c on a.ClassId = c.ID
		left join t_sys_organization_unit d on c.FullCode like d.FullCode + '%' and d.Levels = c.Levels - 3) tab");

            //引入命名空间 using EDP.Common;
            //default:注册表配置中数据库连接配置所对应的"Key"值
            //若没有设置连线字串,则取注册表配置中的默认数据库连接
            //this.SetConnectionString(Config.Database["default"]); 示例项目使用默认数据库连接,不需要设置
        }

        #region **----实体属性---------------------------**
        /// <summary>
        /// 主键ID
        /// </summary>
        [ModelEnitity(ColumnName = "ID")]
        public string ID { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [ModelEnitity(ColumnName = "Name", IsRequired = true, MaxLength = 50)]
        public string Name { get; set; }

        /// <summary>
        /// 编号
        /// <summary>
        [ModelEnitity(ColumnName = "Number")]
        public string Number { get; set; }

        /// <summary>
        /// 性别ID
        /// </summary>
        [ModelEnitity(ColumnName = "GenderId", IsRequired = true)]
        public int? GenderId { get; set; }

        /// <summary>
        /// 年龄
        /// </summary>
        [ModelEnitity(ColumnName = "Age", IsRequired = true)]
        public int? Age { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        [ModelEnitity(ColumnName = "Contact", MaxLength = 50)]
        public string Contact { get; set; }

        /// <summary>
        /// 联系人手机
        /// </summary>
        [ModelEnitity(ColumnName = "ContactMobile", Regex = @"^[1]([3-9])[0-9]{9}$")]
        public string ContactMobile { get; set; }

        /// <summary>
        /// 兴趣爱好
        /// </summary>
        [ModelEnitity(ColumnName = "Hobby", MaxLength = 500)]
        public string Hobby { get; set; }

        /// <summary>
        /// 校区ID
        /// </summary>
        [ModelEnitity(ColumnName = "CampusId")]
        public string CampusId { get; set; }

        /// <summary>
        /// 班级ID
        /// </summary>
        [ModelEnitity(ColumnName = "ClassId", IsRequired = true)]
        public string ClassId { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        [ModelEnitity(ColumnName = "CreateUserId")]
        public string CreateUserId { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        [ModelEnitity(ColumnName = "CreateUserName")]
        public string CreateUserName { get; set; }

        /// <summary>
        /// 创建部门ID
        /// </summary>
        [ModelEnitity(ColumnName = "CreateDepartmentId")]
        public string CreateDepartmentId { get; set; }

        /// <summary>
        /// 创建机构ID
        /// </summary>
        [ModelEnitity(ColumnName = "CreateOrganizationId")]
        public string CreateOrganizationId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [ModelEnitity(ColumnName = "CreateTime")]
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 修改人ID
        /// </summary>
        [ModelEnitity(ColumnName = "ModifyUserId")]
        public string ModifyUserId { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        [ModelEnitity(ColumnName = "ModifyUserName")]
        public string ModifyUserName { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [ModelEnitity(ColumnName = "ModifyTime")]
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [ModelEnitity(ColumnName = "Remarks", MaxLength = 1000)]
        public string Remarks { get; set; }
        #endregion

        #region **----扩展属性---------------------------**
        /// <summary>
        /// 校区名称
        /// </summary>
        public string CampusName { get; set; }

        /// <summary>
        /// 学校名称
        /// </summary>
        public string SchoolName { get; set; }

        /// <summary>
        /// 班级名称
        /// </summary>
        public string ClassName { get; set; }

        /// <summary>
        /// 性别@readonly
        /// </summary>
        public GenderEnum Gender {
            get {
                GenderEnum ret = GenderEnum.None;
                if (this.GenderId.HasValue) {
                    try { ret = (GenderEnum)this.GenderId; }
                    catch { }
                }
                return ret;
            }
        }

        /// <summary>
        /// 性别显示@readonly
        /// </summary>
        public string GenderDisplay {
            get {
                return EnumHelp.GetEnumDescription(this.Gender);
            }
        }

        /// <summary>
        /// 班级全编码
        /// </summary>
        public string ClassFullCode { get; set; }

        /// <summary>
        /// 学校
        /// </summary>
        public Organization.OrganizationModel School { get; set; } = new Organization.OrganizationModel();
        #endregion

        #region **----方法-------------------------------**
        /// <summary>
        /// 创建当前实体对象
        /// </summary>
        /// <param name="db">数据库接口</param>
        public void Create(IDBInstance db = null) {
            if (string.IsNullOrWhiteSpace(this.ID))
                this.ID = Guid.NewGuid().ToString();

            var loginUser = AuthenticationHelp.GetLoginUser(db);
            if (loginUser != null && loginUser.IsLogin) {
                this.CreateUserId = loginUser.UserInfo.ID;
                this.CreateUserName = loginUser.UserInfo.Name;
                this.CreateDepartmentId = loginUser.UserInfo.DepartmentId;
                this.CreateOrganizationId = loginUser.UserInfo.OrganizationId;
            }

            this.CreateTime = DateTime.Now;
            this.ModifyTime = null;
            this.ModifyUserId = null;
            this.ModifyUserName = null;
        }

        /// <summary>
        /// 修改当前实体对象
        /// </summary>
        /// <param name="db">数据库接口</param>
        public void Modify(IDBInstance db = null) {
            this.ModifyTime = DateTime.Now;

            var loginUser = AuthenticationHelp.GetLoginUser(db);
            if (loginUser != null && loginUser.IsLogin) {
                this.ModifyUserId = loginUser.UserInfo.ID;
                this.ModifyUserName = loginUser.UserInfo.Name;
            }

            this.ModifyTime = DateTime.Now;
            this.CreateDepartmentId = null;
            this.CreateOrganizationId = null;
            this.CreateTime = null;
            this.CreateUserId = null;
            this.CreateUserName = null;
        }

        /// <summary>
        /// 修改当前实体对象
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <param name="db">数据库接口</param>
        public void Modify(string keyValue, IDBInstance db = null) {
            this.ID = keyValue;
            this.Modify(db);
        }
        #endregion
    }
}
